﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyDiemDHBK
{
    public partial class ThemThongTin : Form
    {
        string masv, tensv, lopsv;
        DataTable datamonhoc = new DataTable();
        public static string sql = "";

        public ThemThongTin(string masv, string tensv, string lopsv, DataTable dt)
        {
            this.masv = masv;
            this.tensv = tensv;
            this.lopsv = lopsv;
            this.datamonhoc = dt;
            InitializeComponent();
        }

        private void tbdiemcc_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(e.KeyChar >= 48 && e.KeyChar <= 57 || e.KeyChar == 8 || e.KeyChar == 46);
        }

        private void tbdiemgk_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(e.KeyChar >= 48 && e.KeyChar <= 57 || e.KeyChar == 8 || e.KeyChar == 46);
        }

        private void tbdiemck_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(e.KeyChar >= 48 && e.KeyChar <= 57 || e.KeyChar == 8 || e.KeyChar == 46);
        }

        private void ThemThongTin_Load(object sender, EventArgs e)
        {
            lbhoten.Text = "Họ tên:  "+tensv;
            lblop.Text = lopsv;
            lbmasv.Text = "Mã SV:  "+masv;
            int sohang = datamonhoc.Rows.Count;
            int i = 0;
            while (i < sohang)
            {
                cbbmonhoc.Items.Add(datamonhoc.Rows[i][1]);
                i++;
            }
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btok_Click(object sender, EventArgs e)
        {
            string hocky = cbbhocky.Text;
            string monhoc = cbbmonhoc.Text;
            double diemcc = Convert.ToDouble(tbdiemcc.Text);
            double diemgk = Convert.ToDouble(tbdiemgk.Text);
            double diemck = Convert.ToDouble(tbdiemck.Text);
            if (diemcc < 0 || diemgk < 0 || diemck < 0 || diemcc > 10 || diemgk > 10 || diemck > 10)
                MessageBox.Show("Bạn đã nhập điểm sai..!");
            else if (hocky == "" || monhoc == "") MessageBox.Show("Bạn chưa chọn \"học kỳ\" hoặc \"môn học\"..!");
            else {
                if (MessageBox.Show("Bạn có chắc các thông tin muốn thêm..?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string diemchu = "";
                    float diemphay = 0;
                    double tinhdiem = diemcc * 0.2 + diemgk * 0.3 + diemck * 0.5;
                    diemphay = (float)tinhdiem;
                    if (diemphay < 4) diemchu = "F";
                    else if (diemphay >= 4 && diemphay <= 5.4) diemchu = "D";
                    else if(diemphay > 5.4 && diemphay < 7) diemchu = "C";
                    else if(diemphay >= 7 && diemphay < 8.5) diemchu = "B";
                    else diemchu = "A";

                    sql = "insert into Diem values('"+masv+"', (select MaMH from  MonHoc where TenMH like N'"+monhoc+"'), '"+diemcc+
                        "', '" + diemgk + "', '" + diemck + "', '" + diemchu + "', '" + diemphay + "', (select MaHK from  HocKy where TenHK like N'" + hocky + "'))";
                    this.Dispose();
                }
            }
        }
        public string getsql()
        {
            return sql;
        }
    }
}
